package com.cksys.achievement.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cksys.achievement.entity.Type;
import com.cksys.achievement.service.ITypeService;
import com.cksys.achievement.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tadechen
 * @since 2019-09-26
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    ITypeService typeService;

    @GetMapping("/get")
    public RestResponse getTypeList() {
        return RestResponse.ok(typeService.list(new LambdaQueryWrapper<Type>().orderByDesc(Type::getName)));
    }

    @GetMapping("/page")
    public RestResponse page(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                             @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
                             Type type) {
        LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
        if (type.getName() != null) {
            queryWrapper.like(Type::getName, type.getName()).orderByDesc(Type::getName);
        }
        return RestResponse.ok(typeService.page(new Page<>(page, size), queryWrapper));
    }

    /*--------------管理员开始---------------*/

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @GetMapping("/count")
    public RestResponse count() {
        return RestResponse.ok(typeService.count());
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @PostMapping("/add")
    public RestResponse add(@Valid Type type) {
        boolean save = typeService.save(type);
        if (save) {
            return RestResponse.ok(type);
        }
        return RestResponse.fail();
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @PostMapping("/update")
    public RestResponse update(@Valid Type type) {
        boolean save = typeService.updateById(type);
        if (save) {
            return RestResponse.ok(type);
        }
        return RestResponse.fail();
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @PostMapping("/delete/{id}")
    public RestResponse delete(@PathVariable Integer id) {
        boolean b = typeService.deleteType(id);
        if (b) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @PostMapping("/delete/more")
    public RestResponse deleteMore(@RequestParam("ids[]") List<Integer> ids) {
        boolean b = true;
        for (Integer id : ids) {
            b = typeService.deleteType(id);
        }
        if (b) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }


}
